import { GetDatasetTreeItem, GetTargetTreeOriginalTarget } from '@/common';

import { PaginationProps } from '../conmon';
import { VectorModelItem } from '../vector/vector-model';

export enum DatasetType {
  ALL = 'all', // 全部类型
  FILE = 'file', // 文件
  WEB = 'web', // 网页
  API = 'api', // API
  DATABASE = 'database', // 数据库,
  FEISHU = 'feishu', // 飞书,
}

export enum DatasetTypeEnum {
  folder = 'folder',
  dataset = 'dataset',
  function = 'function',
  database = 'database',
  extension = 'extension',
  websiteDataset = 'websiteDataset', // depp link
  externalFile = 'externalFile',
  invisible = 'invisible',
  faq = 'faq',
  api = '',
}

export interface ExtensionCategory {
  category: string;
  page_data: {
    list: Array<any>;
    total: number;
    page: number;
    page_size: number;
  };
}

export enum DatasetStatusEnum {
  active = 'active',
  syncing = 'syncing',
}

export interface DatasetSimpleItem {
  _id: string;
  avatar: string;
  name: string;
  vectorModel: VectorModelItem;
}

export interface DatasetPermission {
  hasManagePer: boolean;
  hasReadPer: boolean;
  hasWritePer: boolean;
  isOwner: boolean;
  value: number;
}

export type PermissionValueType = number;

export interface DatasetListItem extends DatasetSimpleItem {
  parentId: string;
  intro: string;
  type: DatasetTypeEnum;
  permission: DatasetPermission;
  defaultPermission: PermissionValueType;
  children?: DatasetListItem[];
  updateTime: string;
  count?: number;
  fileId?: string;
  forbid: boolean;
  openapiUrl?: string;

  id: string;
  key: string;
  agent_model?: string;
  app_id?: string;
  avatar: string;
  create_time?: Date;
  dataset_type?: Array<string>;
  default_permission?: string;
  group_id?: string;
  inherit_permission?: boolean;
  metadata?: any;
  parent_id?: string | null;
  status?: string;
  team_id?: string;
  tmb_id?: string;
  update_time?: Date;
  vector_model?: string;
}

export interface DatasetListReq {
  parentId: string | null | undefined;
  type?: DatasetTypeEnum;
  action?: string;
}

export interface DatasetUpdateReq {
  id: string;
  name?: string;
  parentId?: string;
}
export interface DatasetDeleteReq {
  id?: string[];
  dataset_type?: string;
  user_id?: string;
  appId?: string;
  reviewStatus?: string;
  datasetId?: string;
  collectionId_list?: string[];
}

export interface DatasetCreateReq {
  type: string;
  avatar?: string;
  name: string;
  intro?: string;
  vectorModel?: string;
  agentModel?: string;
  defaultPermission?: number;
  parentId?: string;
  openapiUrl?: string;
}

export interface FolderCreateReq {
  intro: string;
  name: string;
  parentId: string;
  datasetId?: string;
}

export interface Collection {
  id?: string;
  _id: string;
  collectionId: string;
  tmbId: string;
  fileId: string;
  name: string;
  type: string;
  forbid: boolean;
  parentId?: string;
  permission: DatasetPermission;
  dataAmount: number;
  trainingAmount: number;
  trainingType: string;
  createTime: string;
  updateTime: string;
  fileName: string;
  datasetId?: string;
}

export interface CollectionListReq {
  datasetId: string;
  pageNum: number;
  pageSize: number;
  parentId?: string;
  searchText?: string;
  selectFolder?: boolean;
  simple?: boolean;
}

export interface CollectionListRes {
  data: Collection[];
  pageNum: number;
  pageSize: number;
  total: number;
}

export interface CollectionReadReq {
  collection_id: string;
}

export interface CollectionReadRes {
  type: string;
  value: string;
}

export interface CollectionPreviewInfoRes {
  url: string;
  size: number;
  filename: string;
  preview: boolean;
  show_keyword: Boolean;
  type?: string;
  file_url: string;
}

export interface UploadFileCollection {
  uid?: string;
  datasetId?: string;
  fileId?: string;
  name?: string;
  type?: string;
  size?: number;
  percent?: number;
  status?: string;
  statusCn?: string;
  totalCount?: number;
  successCount?: number;
  pageCount?: number; // 添加页数属性
  isDelete?: boolean;
}

export interface CreateDatasetCollectionReq {
  datasetId: string;
  fileId: string;
  name: string;
  chunkSize: number;
  chunkSplitter: string;
  qaPrompt: string;
  trainingType: string;
  preservePdfImages: string;
  parentId?: string;
}

export interface CreateDatasetItemnReq {
  datasetId: string;
  name: string;
  parentId: string;
  type: string;
}

export interface UpdateDatasetCollectionRes {
  id: string;
  name?: string;
  forbid?: boolean;
  parentId?: string;
}

export interface FcsTaskDatasetRes {
  file_id: string;
  file_name: string;
  status: string;
  message: string;
  length?: number;
}

export interface statusCounter {
  PENDING: number;
  SUCCESS: number;
  FAILURE: number;
  PROGRESS: number;
}

export interface WebLink {
  task_src: any;
  link: string;
  status: string;
  status_cn: string;
  total_count: number;
  status_counter: statusCounter;
  progress: number;
  crawlLinksTotal: string;
  traceback: string;
  crawl_records_id: string;
  submitTime: string;
}

export interface AllWebLinkRes {
  page_cnt: number;
  current_page: number;
  page_size: number;
  total_cnt: number;
  return_cnt: number;
  page_data: WebLink[];
}

// export type DatasetDataListItemType = {
//   _id: string;
//   datasetId: string;
//   collectionId: string;
//   q: string; // embedding content
//   a: string; // bonus content
//   chunkIndex?: number;
//   dataId?: string;
//   // indexes: DatasetDataSchemaType['indexes'];
// };

export type GetDatasetDataListProps = PaginationProps & {
  searchText?: string;
  collectionId?: string;
  collection_id?: string;
};

export type CollectionIndexReq = {
  collection_id: string;
  text: string;
};

export type QA = {
  id?: string;
  q: string;
  a: string;
};

export type InsertFaqReq = {
  dataset_id: string;
  collection_id?: string;
  datas: QA[];
};

export type InsertSameQa = {
  idx: number;
  err: string;
  id: string;
};

export type InsertFaqRes = {
  msg: string;
  fail_list: InsertSameQa[];
};

export type DataIndex = {
  defaultIndex: boolean;
  text: string;
};

export type InsertDataReq = {
  collectionId: string;
  indexes?: DataIndex[];
} & QA;

export interface KnowledgeReq {
  checkStatus?: string;
  page?: number;
  pageSize?: number;
  searchText?: string;
  keywords?: string;
  id?: string;
  type?: string;
  offset?: number;
  group_id?: string;
  app_id?: string;
  appId?: string;
  dataset_id?: string;
  fileIdList?: string[];
  reviewStatus?: string;
  operater?: string;
  reason?: string;
  datasetId?: string[];
  ids?: string[];
  status?: string;
  startTime?: string;
  endTime?: string;
  page_size?: number;
  collectionId?: string[];
  operator?: string;
  groupId?: string;
}

export interface CreateKnowledgeReq {
  parentId?: string | null;
  app_id: string;
  type: string;
  name: string;
  intro?: string;
  avatar?: string;
  vectorModel?: string;
  agentModel?: string;
  openapiUrl?: string;
  dataset_tags?: Array<string>;
  groupId?: string;
  assessor?: Array<[]>;
  administrator?: Array<[]>;
  uploader?: Array<[]>;
  dataset_id?: string;
  dataset_type?: string;
  headerAuth?: any;
  isUpdateChild?: boolean;
}

export interface KnowledgeListReq {
  parentId?: string | null;
  datasetId?: string;
  page?: number;
  page_size?: number;
  searchText?: string;
  offset?: number;
  user_id?: string;
  groupId?: string;
  isPage?: boolean;
  app_id?: string;
  dataset_type?: string;
  type?: string;
  appId?: string;
}
export interface RolesListRes {
  code: string;
  description: string;
  id: string;
  name: string;
  target_users?: any;
}

export interface KnowledgeListResList {
  id: string;
  parentId: null | string;
  team_id: string;
  tmb_id: string;
  type: string;
  status: string;
  avatar: string;
  name: string;
  vector_model: string;
  agent_model: string;
  intro: string;
  default_permission: number;
  inherit_permission: boolean;
  update_time: string;
  create_time: string;
  metadata: {};
  openapiUrl: null;
  app_id: null;
  groupId: null;
  dataset_tags: any[];
}

export interface KnowledgeListRes {
  total: number;
  list: KnowledgeListResList[];
}

export interface GetDatasetReq {
  appId: string;
  type?: string;
  searchText?: string;
}

export interface GetDatasetNl2sqlList {
  _id: string;
  appId: string;
  intro: string;
  type: string;
  name: string;
  created_at: string;
  updated_at: string;
  children: GetDatasetTreeItem[];
  targets: GetTargetTreeOriginalTarget[];
}

export interface GetDatasetNl2sqlRes {
  total: number;
  list: GetDatasetNl2sqlList[];
}

export interface uploadFileReq {
  metadata?: string;
  bucketName: string;
  file: File;
  parent_id: string;
  dataset_id: string;
  app_id: string;
}

export const MAX_NAME_LENGTH = 20;
export const MAX_DOCUMENT_LENGTH = 100;

export const GROUP_ID_UNGROUPED = '681c89e0c4ee0738c3621831';

/* ------------ dataset -------------- */

export const DatasetTypeMap = {
  [DatasetTypeEnum.folder]: {
    icon: 'common/folderFill',
    label: 'folder_dataset',
    collectionLabel: 'common.Folder',
  },
  [DatasetTypeEnum.dataset]: {
    icon: 'core/dataset/commonDatasetOutline',
    label: 'common_dataset',
    collectionLabel: 'common.File',
  },
  [DatasetTypeEnum.function]: {
    icon: 'core/dataset/commonDatasetOutline',
    label: 'common_dataset',
    collectionLabel: 'common.File',
  },
  [DatasetTypeEnum.faq]: {
    icon: 'core/dataset/commonDatasetOutline',
    label: 'common_dataset',
    collectionLabel: 'common.File',
  },
  [DatasetTypeEnum.invisible]: {
    icon: 'core/dataset/commonDatasetOutline',
    label: 'common_dataset',
    collectionLabel: 'common.File',
  },
  [DatasetTypeEnum.websiteDataset]: {
    icon: 'core/dataset/websiteDatasetOutline',
    label: 'website_dataset',
    collectionLabel: 'common.Website',
  },
  [DatasetTypeEnum.externalFile]: {
    icon: 'core/dataset/externalDatasetOutline',
    label: 'external_file',
    collectionLabel: 'common.File',
  },
  [DatasetTypeEnum.extension]: {
    icon: 'core/dataset/externalDatasetOutline',
    label: 'common_dataset',
    collectionLabel: 'common.File',
  },
};

export const DatasetStatusMap = {
  [DatasetStatusEnum.active]: {
    label: 'core.dataset.status.active',
  },
  [DatasetStatusEnum.syncing]: {
    label: 'core.dataset.status.syncing',
  },
};

/* ------------ collection -------------- */
export enum DatasetCollectionTypeEnum {
  folder = 'folder',
  virtual = 'virtual',

  file = 'file',
  link = 'link', // one link
  externalFile = 'externalFile',
}
export const DatasetCollectionTypeMap = {
  [DatasetCollectionTypeEnum.folder]: {
    name: 'core.dataset.folder',
  },
  [DatasetCollectionTypeEnum.file]: {
    name: 'core.dataset.file',
  },
  [DatasetCollectionTypeEnum.externalFile]: {
    name: 'core.dataset.externalFile',
  },
  [DatasetCollectionTypeEnum.link]: {
    name: 'core.dataset.link',
  },
  [DatasetCollectionTypeEnum.virtual]: {
    name: 'core.dataset.Manual collection',
  },
};

export enum DatasetCollectionSyncResultEnum {
  sameRaw = 'sameRaw',
  success = 'success',
}
export const DatasetCollectionSyncResultMap = {
  [DatasetCollectionSyncResultEnum.sameRaw]: {
    label: 'core.dataset.collection.sync.result.sameRaw',
  },
  [DatasetCollectionSyncResultEnum.success]: {
    label: 'core.dataset.collection.sync.result.success',
  },
};

/* ------------ data -------------- */

/* ------------ training -------------- */
export enum ImportDataSourceEnum {
  fileLocal = 'fileLocal',
  fileLink = 'fileLink',
  fileCustom = 'fileCustom',
  csvTable = 'csvTable',
  externalFile = 'externalFile',
}

export enum TrainingModeEnum {
  chunk = 'chunk',
  auto = 'auto',
  qa = 'qa',
}

export const TrainingTypeMap = {
  [TrainingModeEnum.chunk]: {
    label: 'core.dataset.training.Chunk mode',
    tooltip: 'core.dataset.import.Chunk Split Tip',
    openSource: true,
  },
  [TrainingModeEnum.auto]: {
    label: 'core.dataset.training.Auto mode',
    tooltip: 'core.dataset.training.Auto mode Tip',
    openSource: false,
  },
  [TrainingModeEnum.qa]: {
    label: 'core.dataset.training.QA mode',
    tooltip: 'core.dataset.import.QA Import Tip',
    openSource: true,
  },
};

/* ------------ search -------------- */
export enum DatasetSearchModeEnum {
  embedding = 'embedding',
  fullTextRecall = 'fullTextRecall',
  knowledgeGraph = 'knowledgeGraph',
  mixedRecall = 'mixedRecall',
}

export const DatasetSearchModeMap = {
  [DatasetSearchModeEnum.embedding]: {
    icon: 'core/dataset/modeEmbedding',
    title: 'core.dataset.search.mode.embedding',
    desc: 'core.dataset.search.mode.embedding desc',
    value: DatasetSearchModeEnum.embedding,
  },
  [DatasetSearchModeEnum.fullTextRecall]: {
    icon: 'core/dataset/fullTextRecall',
    title: 'core.dataset.search.mode.fullTextRecall',
    desc: 'core.dataset.search.mode.fullTextRecall desc',
    value: DatasetSearchModeEnum.fullTextRecall,
  },
  [DatasetSearchModeEnum.knowledgeGraph]: {
    icon: 'core/dataset/knowledgeGraph',
    title: 'core.dataset.search.mode.knowledgeGraph',
    desc: 'core.dataset.search.mode.knowledgeGraph desc',
    value: DatasetSearchModeEnum.knowledgeGraph,
  },
  [DatasetSearchModeEnum.mixedRecall]: {
    icon: 'core/dataset/mixedRecall',
    title: 'core.dataset.search.mode.mixedRecall',
    desc: 'core.dataset.search.mode.mixedRecall desc',
    value: DatasetSearchModeEnum.mixedRecall,
  },
};

export enum SearchScoreTypeEnum {
  embedding = 'embedding',
  fullText = 'fullText',
  reRank = 'reRank',
  rrf = 'rrf',
  imageEmbedding = 'imageEmbedding',
  graphEntityScore = 'graphEntityScore',
  graphRelationScore = 'graphRelationScore',
  graphTextScore = 'graphTextScore',
}
export const SearchScoreTypeMap = {
  [SearchScoreTypeEnum.embedding]: {
    label: 'core.dataset.search.score.embedding',
    desc: 'core.dataset.search.score.embedding desc',
    showScore: true,
    color: 'var(--blue-5)',
  },
  [SearchScoreTypeEnum.imageEmbedding]: {
    label: 'core.dataset.search.score.imageEmbedding',
    desc: 'core.dataset.search.score.imageEmbedding desc',
    showScore: true,
    color: 'var(--success-color)',
  },
  [SearchScoreTypeEnum.graphEntityScore]: {
    label: 'core.dataset.search.score.graphEntityScore',
    desc: 'core.dataset.search.score.graphEntityScore desc',
    showScore: true,
    color: 'var(--success-color)',
  },
  [SearchScoreTypeEnum.graphRelationScore]: {
    label: 'core.dataset.search.score.graphRelationScore',
    desc: 'core.dataset.search.score.graphRelationScore desc',
    showScore: true,
    color: 'var(--success-color)',
  },
  [SearchScoreTypeEnum.graphTextScore]: {
    label: 'core.dataset.search.score.graphTextScore',
    desc: 'core.dataset.search.score.graphTextScore desc',
    showScore: true,
    color: 'var(--success-color)',
  },
  [SearchScoreTypeEnum.fullText]: {
    label: 'core.dataset.search.score.fullText',
    desc: 'core.dataset.search.score.fullText desc',
    showScore: false,
    color: 'var(--warning-color)',
  },
  [SearchScoreTypeEnum.reRank]: {
    label: 'core.dataset.search.score.reRank',
    desc: 'core.dataset.search.score.reRank desc',
    showScore: true,
    color: 'var(--error-color)',
  },
  [SearchScoreTypeEnum.rrf]: {
    label: 'core.dataset.search.score.rrf',
    desc: 'core.dataset.search.score.rrf desc',
    showScore: false,
    color: 'var(--info)',
  },
};

export const CustomCollectionIcon = 'common/linkBlue';
export const LinkCollectionIcon = 'common/linkBlue';

/* source prefix */
export enum DatasetSourceReadTypeEnum {
  fileLocal = 'fileLocal',
  link = 'link',
  externalFile = 'externalFile',
}

export interface UpdateFileReq {
  appId: string;
  parentId: string;
  id: string;
}
